package com.toidep.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.transition.ChangeBounds;
import android.util.Log;
import android.widget.Toast;

import com.toidep.config.Constants;
import com.toidep.entities.AlbumEntity;
import com.toidep.entities.ArticleEntity;
import com.toidep.entities.CityEntity;
import com.toidep.entities.CommentEntity;
import com.toidep.entities.DetailEmailEntity;
import com.toidep.entities.EmailEntity;
import com.toidep.entities.FreeEntity;
import com.toidep.entities.GalleryEntity;
import com.toidep.entities.ModelEntity;
import com.toidep.entities.ProfileEntity;
import com.toidep.entities.TagEntity;
import com.toidep.entities.UpdateEntity;
import com.toidep.entities.VideoEntity;
import com.toidep.preference.SharePreference;
import com.toidep.utilities.StringUtility;

@SuppressLint("NewApi")
public class ParseJSON {

	/*********************** Functions Utilities *********************/

	/**
	 * JSON: Get Boolean theo key
	 */
	public static boolean getBooleanValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? false : obj.getBoolean(key);
		} catch (JSONException e) {
			return false;
		}
	}

	/**
	 * JSON: Get Int theo key
	 */
	public static int getIntValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? 0 : obj.getInt(key);
		} catch (JSONException e) {
			return 0;
		}
	}

	/**
	 * JSON: Get Long theo key
	 */
	public static Long getLongValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? 0 : obj.getLong(key);
		} catch (JSONException e) {
			return 0l;
		}
	}

	/**
	 * JSON: Get String theo key
	 */
	public static String getStringValue(JSONObject obj, String key) {
		try {
			String result = obj.isNull(key) ? "" : obj.getString(key);
			return result;
		} catch (JSONException e) {
			return "";
		}
	}

	/**
	 * JSON: Get JSONObject theo key
	 */
	public static JSONObject getJsonObject(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? null : obj.getJSONObject(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	/**
	 * JSON: Get JsonArrray theo key
	 */
	public static JSONArray getJsonArray(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? null : obj.getJSONArray(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	/**
	 * Get ket qua cua mot response co Error hay khong?
	 * @param response
	 * @return
	 */
	private static JSONObject checkResult(String response) {
		try {
			if (response != null && response != "" ) {
				JSONObject jsonObject = new JSONObject(response);
				if (jsonObject != null) {
					int code = getIntValue(jsonObject, Constants.PARAM_RESPONSE_CODE);
					if (code > 0) {
						return jsonObject;
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	/*********************** End Functions Utilities *********************/
	
	/**
	 * Lay danh sach News vao list
	 * @param response
	 * @return
	 */
	public static ArrayList<ArticleEntity> getAllNewsObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<ArticleEntity> resultList = null;

		if (responseObj != null) {
			try {
				JSONArray jsonArray = getJsonArray(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<ArticleEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						ArticleEntity item = new ArticleEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	/**
	 * Lay danh sach Models vao list
	 * @param response
	 * @return
	 */
	public static ArrayList<ModelEntity> getAllModelObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<ModelEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_MODEL);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<ModelEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						ModelEntity item = new ModelEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	/**
	 * Lay danh sach Videos vao list
	 * @param response
	 * @return
	 */
	public static ArrayList<VideoEntity> getAllVideoObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<VideoEntity> resultList = null;

		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_VIDEOS);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<VideoEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						VideoEntity item = new VideoEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	/**
	 * Lay chi tiet video
	 * @param response
	 * @return
	 */
	public static VideoEntity getVideoDetailObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		VideoEntity resultVideo = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				resultVideo = new VideoEntity();
				resultVideo.setInfo(dataObj);
				dataObj = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultVideo;
	}
	
	/**
	 * Lay danh sach Tags vao list
	 * @param jsonArray
	 * @return
	 */
	public static ArrayList<TagEntity> getAllTagObject(JSONArray jsonArray) {
		ArrayList<TagEntity> resultList = null;

		if (jsonArray != null && jsonArray.length() > 0) {
			try {
				resultList = new ArrayList<TagEntity>();
				JSONObject jsonObject = null;
				for (int i = 0; i < jsonArray.length(); i++) {
					jsonObject = (JSONObject) jsonArray.get(i);
					TagEntity item = new TagEntity();
					item.setInfo(jsonObject);
					resultList.add(item);
					item = null;
				}
				jsonObject = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	/**
	 * Lay danh sach Albums vao list
	 * @param response
	 * @return
	 */
	public static ArrayList<AlbumEntity> getAllAlbumObjectsFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<AlbumEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_ALBUMS);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<AlbumEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						AlbumEntity item = new AlbumEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	
	//QuangLM_20140507 fix bug #219
	  //start
	/**
	 * Get Field named "thum" instead of "thums"
	 * @param response
	 * @return
	 */
	public static ArrayList<AlbumEntity> getAllAlbumObjectsFromResponseFix219(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<AlbumEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_ALBUMS);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<AlbumEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						AlbumEntity item = new AlbumEntity();
						item.setInfoFix219(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	//end
	
	
	/**
	 * Lay thong tin chi tiet cua Album
	 * @param response
	 * @return
	 */
	public static AlbumEntity getAlbumObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		AlbumEntity resultAlbum = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				resultAlbum = new AlbumEntity();
				resultAlbum.setInfo(dataObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return resultAlbum;
	}
	
	/**
	 * Lay thong tin chi tiet cua Model
	 * @param response
	 * @return ModelEntity
	 */
	public static ModelEntity getModelObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ModelEntity modelEntity = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				modelEntity = new ModelEntity();
				modelEntity.setInfo(dataObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return modelEntity;
	}
	
	public static ArrayList<CityEntity> getCityObjectFromResponse(
			String response) {
		JSONObject responseObj;
		try {
			responseObj = new JSONObject(response);
			ArrayList<CityEntity> resultList = null;
			if (responseObj != null) {

				JSONArray jsonArray = getJsonArray(responseObj,
						Constants.PARAM_RESPONSE_DATA);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<CityEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						CityEntity item = new CityEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			}
			return resultList;
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}

	}
	/**
	 * Lay danh sach comments
	 * @param response
	 * @return ModelEntity
	 */
	public static ArrayList<CommentEntity> getAllCommentsFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<CommentEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_COMMENT);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<CommentEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						CommentEntity item = new CommentEntity();
						item.setInfo(jsonObject);
						
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	/**
	 * Lay danh sach article (Tin tuc, Cau chuyen, Su kien) 
	 * @param response
	 * @return
	 */
	public static ArrayList<ArticleEntity> getAllArticlesObjectFromResponse(int articleType, String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<ArticleEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				String articleParam = "";
				if (articleType == Constants.ARTICLE_MODE_NEWS) {
					articleParam = Constants.PARAM_RESPONSE_NEWS;
				} else if (articleType == Constants.ARTICLE_MODE_STORY) {
					articleParam = Constants.PARAM_RESPONSE_STORY;
				} else if (articleType == Constants.ARTICLE_MODE_EVENT) {
					articleParam = Constants.PARAM_RESPONSE_EVENT;
				}
				
				JSONArray jsonArray = getJsonArray(dataObj, articleParam);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<ArticleEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						ArticleEntity item = new ArticleEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return resultList;
	}

	public static void getUserInfoFromResponse(Activity activity, String response) {
		JSONObject responseObj = checkResult(response);
		
		if (responseObj != null) {
			SharePreference ioeShare = SharePreference.getInstance(activity);
			try {
				JSONObject dataObject = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObject != null) {
										
					ioeShare.putLoginStatus(true);
					ioeShare.putUserId(getStringValue(dataObject, Constants.PARAM_RESPONSE_ID));
					ioeShare.putUserName(getStringValue(dataObject, Constants.PARAM_RESPONSE_USER_NAME));
					ioeShare.putUserFullName(getStringValue(dataObject, Constants.PARAM_RESPONSE_FULL_NAME));
					ioeShare.putUserEmail(getStringValue(dataObject, Constants.PARAM_RESPONSE_EMAIL));
					ioeShare.putUserAvatar(StringUtility.parseImageUrl(getStringValue(dataObject, Constants.PARAM_RESPONSE_AVATAR)));
					ioeShare.putUserPhone(getStringValue(dataObject, Constants.PARAM_RESPONSE_PHONE));
					ioeShare.putAccessToken(getStringValue(dataObject, Constants.PARAM_RESPONSE_TOKEN));
					Log.e("accesstoken after login", getStringValue(dataObject, Constants.PARAM_RESPONSE_TOKEN));
					ioeShare.putLastTimeLogin(getLongValue(dataObject, Constants.PARAM_RESPONSE_LAST_LOGIN));
				} else {
					ioeShare.putLoginStatus(false);
				}
			} catch (Exception e) {
				ioeShare.putLoginStatus(false);
				e.printStackTrace();
			}
		}
	}

	public static String changeFormatStringDateTime(String time) {
		String inputPattern = "yyyy-MM-dd HH:mm:ss";
		String outputPattern = "dd-MM-yyyy";
		SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
		SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

		Date date = null;
		String str = null;

		try {
			date = inputFormat.parse(time);
			str = outputFormat.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}
	public static void getUserAllInfoFromResponse(Activity activity, String response) {
		JSONObject responseObj = checkResult(response);
		
		if (responseObj != null) {
			SharePreference ioeShare = SharePreference.getInstance(activity);
			try {
				JSONObject dataObject = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObject != null) {
					ioeShare.putUserId(getStringValue(dataObject, Constants.PARAM_RESPONSE_ID));
					ioeShare.putUserName(getStringValue(dataObject, Constants.PARAM_RESPONSE_USER_NAME));
					ioeShare.putUserFullName(getStringValue(dataObject, Constants.PARAM_RESPONSE_FULL_NAME));
					ioeShare.putUserEmail(getStringValue(dataObject, Constants.PARAM_RESPONSE_EMAIL));
					ioeShare.putUserPhone(getStringValue(dataObject, Constants.PARAM_RESPONSE_PHONE));
					ioeShare.putAccessToken(getStringValue(dataObject, Constants.PARAM_RESPONSE_TOKEN));
					ioeShare.putUserGender(getIntValue(dataObject, Constants.PARAM_RESPONSE_GENDER));
					String birthday = getStringValue(dataObject, Constants.PARAM_RESPONSE_BIRTHDAY);
					// 18-05-2014 Bugs # 308 start.
					if (!birthday.equals("")) {
						birthday = changeFormatStringDateTime(birthday);
					}
//					Log.e("ConvertBD", birthday);
					ioeShare.putUserBirthday(birthday);

					// 18-05-2014 End.
					ioeShare.putLastTimeLogin(getLongValue(dataObject, Constants.PARAM_RESPONSE_LAST_LOGIN));
					ioeShare.putUserProvince(getStringValue(dataObject, Constants.PARAM_RESPONSE_PROVINCE));
				} else {
					ioeShare.putLoginStatus(false);
				}
			} catch (Exception e) {
				ioeShare.putLoginStatus(false);
				e.printStackTrace();
			}
		}
	}

	/**
	 * Lay profile cua nguoi mau
	 * @param response
	 * @return
	 */
	public static ProfileEntity getProfile(String response) {
		JSONObject responseObj = checkResult(response);
		
		ProfileEntity profileEntity =  null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				profileEntity = new ProfileEntity();
				profileEntity.setInfo(dataObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return profileEntity;
	}

	/**
	 * Lay chi tiet tin tuc, su kien, cau chuyen
	 * @param response
	 * @return
	 */
	public static ArticleEntity getNewsDetailFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArticleEntity articleEntity = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				articleEntity = new ArticleEntity();
				articleEntity.setInfo(dataObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return articleEntity;
	}
	
	/**
	 * Lay danh sach Galleries vao list
	 * @param jsonArray
	 * @return
	 */
	public static ArrayList<GalleryEntity> getAllGalleryObject(JSONArray jsonArray) {
		ArrayList<GalleryEntity> resultList = null;

		if (jsonArray != null && jsonArray.length() > 0) {
			try {
				resultList = new ArrayList<GalleryEntity>();
				JSONObject jsonObject = null;
				for (int i = 0; i < jsonArray.length(); i++) {
					jsonObject = (JSONObject) jsonArray.get(i);
					GalleryEntity item = new GalleryEntity();
					item.setInfo(jsonObject);
					resultList.add(item);
					item = null;
				}
				jsonObject = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}

	/**
	 * Lay ket qua post comment
	 * @param response
	 * @return true | false
	 */
	public static boolean getResultPostCommentFromResponse(String response) {
		if (response != null && response != "") {
			try {
				JSONObject jsonObject = new JSONObject(response);
				int code = jsonObject.getInt(Constants.PARAM_RESPONSE_CODE);
				if (code > 0) 
					return true;
			} catch (JSONException e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	/**
	 * Lay thong tin Free
	 * @param response
	 * @return
	 */
	public static FreeEntity getFreeObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
//		Log.d("Q_", "getFreeObjectFromResponse : " + response);
		FreeEntity result = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				result = new FreeEntity();
				result.setInfo(dataObj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Log.d("Q_", "getFreeObjectFromResponse : check size = " + result.getListFree().size());
		return result;
	}
	
	/**
	 * Lay danh sach Update
	 * @param responseArray
	 * @return
	 */
	public static ArrayList<UpdateEntity> getAllUpdateObjectFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<UpdateEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONArray jsonArray = getJsonArray(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<UpdateEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						UpdateEntity item = new UpdateEntity();
						item.setInfo(jsonObject);
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}

	public static ResultObject getPaymentResult(String response) {
		//Bug #228 - start
		ResultObject resultObject = null;
		try {
			if (response != null && response != "" ) {
				JSONObject jsonObject = new JSONObject(response);
				if (jsonObject != null) {
					int code = getIntValue(jsonObject, Constants.PARAM_RESPONSE_CODE);
					String message = getStringValue(jsonObject, Constants.PARAM_RESPONSE_MESSAGE);
					String amount ="";
					if(code ==1){
						JSONObject data = jsonObject.getJSONObject(Constants.PARAM_RESPONSE_DATA);
						 amount = getStringValue(data,Constants.PARAM_RESPONSE_AMOUNT);
					}
					resultObject = new ResultObject(code, message,amount);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		//Bug #228 - end
		return resultObject;
	}

	public static ArrayList<EmailEntity> getAllEmailsFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		ArrayList<EmailEntity> resultList = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_EMAIL);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<EmailEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						EmailEntity item = new EmailEntity();
						item.setInfo(jsonObject);
						
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return resultList;
	}
	
	public static DetailEmailEntity getDetailEmailsFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		DetailEmailEntity detail = null;
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				detail = new DetailEmailEntity();
				detail.setInfo(dataObj);
				/*JSONArray jsonArray = getJsonArray(dataObj, Constants.PARAM_RESPONSE_EMAIL);
				if (jsonArray != null && jsonArray.length() > 0) {
					resultList = new ArrayList<EmailEntity>();
					JSONObject jsonObject = null;
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = (JSONObject) jsonArray.get(i);
						EmailEntity item = new EmailEntity();
						item.setInfo(jsonObject);
						
						resultList.add(item);
						item = null;
					}
					jsonObject = null;
				}*/
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return detail;
	}

	public static String getTextDataFromResponse(String response) {
		JSONObject responseObj = checkResult(response);
		
		if (responseObj != null) {
			try {
				JSONObject dataObj = getJsonObject(responseObj, Constants.PARAM_RESPONSE_DATA);
				if (dataObj == null)
					return null;
				
				return getStringValue(dataObj, Constants.PARAM_RESPONSE_TEXT);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}
}
